package com.zztl.infiniteBand.ui.view.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.uuzuche.lib_zxing.activity.CodeUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseActivity


/**
 * Created by lsm on 2016/9/9.
 */

class InviteDialog : Dialog {


    private val context: BaseActivity? = null
    private val mParams: WindowManager.LayoutParams? = null


    private var mDismissListener: DismissListener? = null

    constructor(context: Context) : super(context) {}

    constructor(context: Context, themeResId: Int) : super(context, themeResId) {}

    class Builder(private val context: Context) {

        private var isCancelable = true
        private var isCancelOutside = true
        private var mDismissListener: DismissListener? = null

         var mIvUserHeader: ImageView?=null
         var mTvUserName: TextView?=null
         var mIvQrCode: ImageView?=null

        private var name: String? = null
        private var imgUrl: String? = null
        private var qrcodeInfo: String? = null


        fun setName(name: String): Builder {
            this.name = name
            return this
        }

        fun setImgUrl(imgUrl: String): Builder {
            this.imgUrl = imgUrl
            return this
        }

        fun setQrcodeInfo(qrcodeInfo: String): Builder {
            this.qrcodeInfo = qrcodeInfo
            return this
        }

        /**
         * 设置是否可以按返回键取消
         *
         * @param isCancelable
         * @return
         */

        fun setCancelable(isCancelable: Boolean): Builder {
            this.isCancelable = isCancelable
            return this
        }

        /**
         * 设置是否可以取消
         *
         * @param isCancelOutside
         * @return
         */
        fun setCancelOutside(isCancelOutside: Boolean): Builder {
            this.isCancelOutside = isCancelOutside
            return this
        }

        fun setDismissListener(dismissListener: DismissListener): Builder {
            this.mDismissListener = dismissListener
            return this
        }


        fun create(): InviteDialog {
            val inflater = LayoutInflater.from(context)
            val view = inflater.inflate(R.layout.pop_invite, null)
            mTvUserName = view.findViewById(R.id.tv_user_name)
            mIvUserHeader = view.findViewById(R.id.iv_user_header)
            mIvQrCode = view.findViewById(R.id.iv_qr_code)
            val loadingDailog = InviteDialog(context, R.style.DialogStyle)
            loadingDailog.window!!.decorView.rootView.isDrawingCacheEnabled = true
            loadingDailog.window!!.decorView.rootView.buildDrawingCache()
            loadingDailog.setContentView(view)
            loadingDailog.setCancelable(isCancelable)
            loadingDailog.setCanceledOnTouchOutside(isCancelOutside)
            loadingDailog.setUserName(mTvUserName!!, name)
            loadingDailog.setUserHeader(mIvUserHeader!!, imgUrl)
            loadingDailog.setQrCode(mIvQrCode!!, qrcodeInfo)
            loadingDailog.setDismissListener(mDismissListener)
            return loadingDailog

        }


    }


    fun setUserName(view: TextView, name: String?) {
        if (name != null) {
            view.text = name
        }


    }

    fun setUserHeader(view: ImageView, imgUrl: String?) {
        if (imgUrl != null) {
            Glide.with(context!!).load(imgUrl)
                    .apply(RequestOptions().placeholder(R.mipmap.ic_defaultphoto))
                    .into(view)
        }

    }

    fun setQrCode(view: ImageView, codeInfo: String?) {
        if (codeInfo != null) {
            val bitmap = CodeUtils.createImage(codeInfo, view.measuredWidth, view.measuredHeight, BitmapFactory.decodeResource(context!!.resources, R.mipmap.ic_launcher))
            view.setImageBitmap(bitmap)
        }

    }

    private fun setDismissListener(dismissListener: DismissListener?) {
        this.mDismissListener = dismissListener
    }

    fun showDialog() {
        val window = this.window
        val lp = window!!.attributes
        /*
         * 设置宽高，不设置默认根据内容自适应
         */
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.MATCH_PARENT
        window.attributes = lp
        show()
    }


}